import wepy from 'wepy';

import * as OauthApi from './OauthApi'

/**
 * @description 微信创建预付单--在线支付
 * @param orderSn
 *  @param openid
 * 
 */
export async function wxUniformOrders(orderSn, openid) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 2) {
            times = arguments[2];
        }
        let result = await OauthApi.postWithToken(config.BASE_URL + "/wx/wxUniformOrders", {
            orderSn: orderSn,
            openid: openid,
        });
        if (result.error && result.error !== 'success') {
            if (times < 1) {
                if (result.error === 'server_error')
                    return wxUniformOrders(orderSn, openid, ++times);
                else if (result.error === 'invalid_token' || token.error === 'invalid_grant') {
                    OauthApi.resetAccessToken();
                    return wxUniformOrders(orderSn, openid, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 微信支付--预付单
 * @param {String} orderSn 订单id
 * @param {String} openid openid
 */
export async function wxConcurPay(concurLogSn, openid) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 2) {
            times = arguments[2];
        }
        let result = await OauthApi.postWithToken(config.BASE_URL + "/wx/wxUniformConcur", {
            concurLogSn: concurLogSn,
            openid: openid
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return wxConcurPay(concurLogSn, openid, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return wxConcurPay(concurLogSn, openid, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}
/**
 * @description 订单列表
 * @param {string} shopId 店铺序号
 * @param {number} orderStatus 订单状态 0 待支付 2 店家取消 3 客户取消 10 待发货 11 店家退款 12 客户退款 20 已经发货/消费
 * @param {number} orderType 订单类型 1 商品订单 2 服务订单 3 预定订单'
 * @param {number} hadRate 是否评价 0否 1是
 * @param {number} payed 是否付款 0否 1是'
 * @param {number} page  页码(从1开始)
 * @param {number} pageSize 页码大小
 */
export async function orderState(page, pageSize, order_status) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 7) {
            times = arguments[7];
        }
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v2/order/myorder", {

            order_status: order_status,
            page: page,
            pageSize: pageSize,
        });
        if (result.error && result.error !== 'success') {
            if (times < 7) {
                if (result.error === 'server_error')
                    return orderState(page, pageSize, order_status, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return orderState(page, pageSize, order_status, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}
/**
 * @description 普通订单和限时抢购订单下单接口
 * @param jsonProducts 	[{"pid": xxxxx, "quantity": xxxxx}]
 *  @param promotionType 	促销类型 0 普通 1限时促销
 * @param address_id
 * @param coupon_id 	优惠券id
 *  @param promotion_costs 优惠金额 如果没有 写0
 * 
 */
export async function createOrder(jsonProducts, promotionType, address_id, coupon_id, promotion_costs,products_json) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 3) {
            times = arguments[3];
        }
        let result = await OauthApi.postWithToken(config.BASE_URL + "/api/v2/order/create", {
            address_id: address_id,
            jsonProducts: jsonProducts,
            promotionType: promotionType,
            coupon_id: coupon_id,
            promotion_costs: promotion_costs,
            products_json: products_json,

        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return createOrder(shopId, jsonProducts, promotionType,products_json, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return createOrder(shopId, jsonProducts, promotionType,products_json, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}
/**
 * @description 结算页面[查询商品列表]
 * @param {String} pids 订单中包含的商品id
 * @param {String} ppid 促销id
 */
export async function getProductList(pids, ppid) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 2) {
            times = arguments[2];
        }
        let result = await OauthApi.getWithToken(config.BASE_URL + "/api/v2/order/productList", {
            pids: pids,
            ppid: ppid
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return getProductList(pids, ppid, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return getProductList(pids, ppid, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}


/**
 * @description 取消订单
 * @param  orderSn {int} 地址id
 * 
    
 */
export async function removeorder(orderSn) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 1) {
            times = arguments[1];
        }
        let result = await OauthApi.postWithToken(config.BASE_URL + "/api/v2/order/delete", {
            orderSn: orderSn,
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return removeorder(orderSn, ++times);
                else if (result.error === 'invalid_token' || token.error === 'invalid_grant') {
                    OauthApi.resetAccessToken();
                    return removeorder(orderSn, ++times);
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}